package com.yan.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.w3c.dom.DocumentType;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.swing.text.Document;
import java.nio.file.Paths;
import java.util.ArrayList;

import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;

/**
 * @author ：Yan Guang
 * @date ：Created in 2020/11/19 20:38
 * @description：
 */
@Configuration
@EnableSwagger2         //开启swagger2
public class SwaggerConfig {

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }


    //配置了Swagger的docket的bean实例
    @Bean
    public Docket getDocket(Environment environment){

        //设置要显示的swagger环境
        Profiles profiles=Profiles.of("dev","test");
        //获取项目的环境,返回值就可以判断是否处理上述阶段
        boolean flag = environment.acceptsProfiles(profiles);

        //获取当前生产环境dev还是pro

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getapiinfo())
                .groupName("Yan")
                .enable(flag)//这里只有是上述配置的环境的时候才用到swagger
                .select()
                //RequestHandlerSelectors配置要扫描的接口的方式
                //basepackage配置需要扫描的包
                //any()扫描全部
                //withclassannotation扫描类上的注解
                .apis(RequestHandlerSelectors.basePackage("com.yan.swagger.controller"))
//                .paths(PathSelectors.ant("/yan/**"))//过滤的路径,表示只扫描/yan路径下面的接口,其他的都过滤掉
                .build();//工厂模式
    }

    //配置swagger信息=apiinfo
    private ApiInfo getapiinfo(){
        //作者信息
        Contact contact = new Contact("闫广", "https://gitee.com/wodeyanguang", "1825115398@qq.com");
        return new ApiInfo("Yan Documentation",
                "Yan Documentation",
                "1.0",
                "https://gitee.com/wodeyanguang",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }
}
